﻿#region Copyright (c) 2000-2008 Developer Express Inc.
/*
{*******************************************************************}
{                                                                   }
{       Developer Express .NET Component Library                    }
{       AgDataGrid                                 }
{                                                                   }
{       Copyright (c) 2000-2008 Developer Express Inc.              }
{       ALL RIGHTS RESERVED                                         }
{                                                                   }
{   The entire contents of this file is protected by U.S. and       }
{   International Copyright Laws. Unauthorized reproduction,        }
{   reverse-engineering, and distribution of all or any portion of  }
{   the code contained in this file is strictly prohibited and may  }
{   result in severe civil and criminal penalties and will be       }
{   prosecuted to the maximum extent possible under the law.        }
{                                                                   }
{   RESTRICTIONS                                                    }
{                                                                   }
{   THIS SOURCE CODE AND ALL RESULTING INTERMEDIATE FILES           }
{   ARE CONFIDENTIAL AND PROPRIETARY TRADE                          }
{   SECRETS OF DEVELOPER EXPRESS INC. THE REGISTERED DEVELOPER IS   }
{   LICENSED TO DISTRIBUTE THE PRODUCT AND ALL ACCOMPANYING .NET    }
{   CONTROLS AS PART OF AN EXECUTABLE PROGRAM ONLY.                 }
{                                                                   }
{   THE SOURCE CODE CONTAINED WITHIN THIS FILE AND ALL RELATED      }
{   FILES OR ANY PORTION OF ITS CONTENTS SHALL AT NO TIME BE        }
{   COPIED, TRANSFERRED, SOLD, DISTRIBUTED, OR OTHERWISE MADE       }
{   AVAILABLE TO OTHER INDIVIDUALS WITHOUT EXPRESS WRITTEN CONSENT  }
{   AND PERMISSION FROM DEVELOPER EXPRESS INC.                      }
{                                                                   }
{   CONSULT THE END USER LICENSE AGREEMENT FOR INFORMATION ON       }
{   ADDITIONAL RESTRICTIONS.                                        }
{                                                                   }
{*******************************************************************}
*/
#endregion Copyright (c) 2000-2008 Developer Express Inc.

using System.Windows.Controls;
using System.Windows.Input;
using System.Windows;
using System.Collections.Generic;
using DevExpress.AgDataGrid.Data;
using System;
namespace DevExpress.AgDataGrid {
	public interface IAgDataGridTotalCellOwner {
		void TotalCellStyleChanged();
		Canvas Surface { get; }
		List<SummaryItemType> SummaryList { get; set; }
		List<SummaryItemType> UnavailableList { get; }
	}	
	public class AgDataGridTotalCell : AgColumnCellBase, IAgDataGridTotalSelectionOwner {
		IAgDataGridTotalCellOwner owner;
		AgDataGridTotalSelection currentTotalSelection;
		public AgDataGridTotalCell(AgDataGridColumn column) 
			: base(column) {
			DefaultStyleKey = typeof(AgDataGridTotalCell);
			this.owner = column as IAgDataGridTotalCellOwner;
			MouseLeftButtonDown += new MouseButtonEventHandler(AgDataGridTotalCell_MouseLeftButtonDown);
		}
		protected internal Canvas Surface { get { return Owner.Surface; } }
		public IAgDataGridTotalCellOwner Owner { get { return owner; } }
		void AgDataGridTotalCell_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) {
			OnMouseClick(e.GetPosition(Surface));
		}
		protected internal void OnMouseClick(Point position) {
			if(currentTotalSelection == null) {
				BuildSelectTotalList(position);
			}
		}
		protected void BuildSelectTotalList(Point position) {
			currentTotalSelection = new AgDataGridTotalSelection(this, Owner.SummaryList, Owner.UnavailableList);
			currentTotalSelection.SetValue(Canvas.LeftProperty, position.X);
			currentTotalSelection.SetValue(Canvas.TopProperty, position.Y);
			Surface.Children.Add(currentTotalSelection);
		}
		protected void EndTotalSelection(object source) {
			ApplyTotalSelection(currentTotalSelection.GetSummaryInfo());
			HideTotalSelection(currentTotalSelection);
		}
		protected override Size MeasureOverride(Size availableSize) {
			Size result = base.MeasureOverride(availableSize);
			result = new Size(Math.Ceiling(result.Width), Math.Ceiling(result.Height));
			return result;
		}
		protected void ApplyTotalSelection(List<SummaryItemType> items) {
			Owner.SummaryList = items;
		}
		protected internal void HideTotalSelection(AgDataGridTotalSelection totalSelection) {
			Surface.Children.Remove(totalSelection);
			currentTotalSelection = null;
		}
		#region IAgDataGridTotalSelectionOwner Members
		void IAgDataGridTotalSelectionOwner.EndTotalSelection(AgDataGridTotalSelection totalSelection) {
			EndTotalSelection(totalSelection);
		}
		#endregion
	}
}
